package com.hospital.diagnose.impl;


import com.alibaba.fastjson.JSON;
import com.hospital.diagnose.dao.MedicalRecordMapper;
import com.hospital.diagnose.dao.PatientMapper;
import com.hospital.diagnose.dao.TransferMapper;
import com.hospital.diagnose.entity.MedicalRecord;
import com.hospital.diagnose.entity.Patient;
import com.hospital.diagnose.service.DiagnoseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class DiagnoseServiceImpl implements DiagnoseService{


    @Autowired
    PatientMapper patientMapper;
    @Autowired
    MedicalRecordMapper medicalRecordMapper;
    @Autowired
    TransferMapper transferMapper;


/**
     * @Description:增加门诊信息
     * @Param:
     * @return:
     * @Date: 2019/1/2 16:54
     */

@Override
    public String insertPat( String patName, Integer patSex, Integer patAge, String patContactInfo, String patNation,
                          String patDiseaseHistory) {
        TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
        Date nowTime = new Date();
        System.out.println(nowTime);
        String string=new SimpleDateFormat("yyyyMMddHHmmss").format(nowTime);

        Patient patient = new Patient();
        patient.setPatName(patName);
        patient.setPatSex(patSex);
        patient.setPatAge(patAge);
        patient.setPatContactInfo(patContactInfo);
        patient.setPatNation(patNation);
        patient.setPatConven(string);
        patient.setPatDiseaseHistory(patDiseaseHistory);
        patientMapper.insert(patient);
        return selectAllPat();


    }

/*    @Override
    public String deletePat(Integer id) {

        patientMapper.deleteByPrimaryKey(id);
        return null;
    }*/

    public Map selectAll(Patient patient) {
        String sex;
        Map<String,String>map;
        map=new HashMap<>();
        map.put("id",patient.getId().toString());
        map.put("patName",patient.getPatName());
        if (patient.getPatSex()==0){
            sex="女";
        }else {
            sex="男";
        }
        map.put("patSex",sex);
        map.put("patAge",patient.getPatAge().toString());
        map.put("patContactInfo",patient.getPatContactInfo());
        map.put("patNation",patient.getPatNation());
        map.put("patDiseaseHistory",patient.getPatDiseaseHistory());
        map.put("patConven",patient.getPatConven());
        return map;

    }
   @Override
    public String selectAllPat(){
        List<Map<String,String>>resaultmap=new ArrayList <>();
        List<Patient>patients=patientMapper.selectAllPat();
        for (Patient patient:patients){
            resaultmap.add(selectAll(patient));
        }
        return JSON.toJSONString(resaultmap);
    }


    @Override
    public String updatePatByPatient(Patient patient) {

        patientMapper.updateByPrimaryKeySelective(patient);
        return selectAllPat();
    }

    @Override
    public Patient selectByPrimaryKey(Integer id) {
        Patient patient=patientMapper.selectByPrimaryKey(id);
        return  patient;
    }
@Override
public void deleteMedRecordByPatId(Integer mdcRecPatId){
          medicalRecordMapper.deleteByPatId(mdcRecPatId);
}

    @Override
    public void deleteTransferbyByPatId(Integer transPatId) {
        transferMapper.deleteByPatId(transPatId);
    }



    @Override
    public String deleteById(Integer transPatId) {
        transferMapper.deleteByPatId((transPatId));
        medicalRecordMapper.deleteByPatId(transPatId);
        patientMapper.deleteByPrimaryKey(transPatId);
        return selectAllPat();
    }



    /*@Override
    public void selectByConven(String conven){
      Patient patient= patientMapper.selectByPatConven(conven);
        System.out.println(patient);
    }*/

    /*@Override
    public String selectByMdcRecCreateDate(Timestamp mdcRecCreateTime){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = formatter.format(mdcRecCreateTime);

        List<MedicalRecord> medicalRecords=MedicalRecordMapper.selectByMdcRecCreateDate();
        List<Map<String,String>>resaultmap=new ArrayList<>();

        return JSON.toJSONString(resaultmap);
    }*/

    /*@Override
    public Patient selectByPrimaryKey(Integer  id){

        Patient patient=patientMapper.deleteByPrimaryKey(id);

        return patient;
    }*/


}

